import { HealthCheckWithScreenTask } from "./health.check.class";
import { V3proSubjectMessage } from "../model/v3pro.model";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import { time2Millis } from "../../../utils/cmm.util";

let lastStartTime = 0;

export class V3proCheckStartTimeTask extends HealthCheckWithScreenTask<string | undefined> {
  getInterval(): number {
    return time2Millis("5m");
  }

  async checkResult(
    keyParams: string | undefined,
    screenId: string,
    allStatus: V3proScreenAllStatus,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<V3proSubjectMessage> {
    const message = new V3proSubjectMessage();
    message.param = keyParams;
    return message;
  }

  async genKeyParams(
    allStatus: V3proScreenAllStatus,
    screen: V3proScreen,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<string | undefined> {
    const t = allStatus.status.uptimeS * 1000;
    if (t < 0) {
      lastStartTime = 0;
      return undefined;
    }
    const st = new Date().getTime() - t;
    if (Math.abs(st - lastStartTime) < time2Millis("10m")) {
      return new Date(lastStartTime).toISOString();
    }
    lastStartTime = st;
    return new Date(st).toISOString();
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return `notify/status/ssads/device-common:power-on-time/${screenId}`;
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum | undefined {
    return V3proAlertMessageErrTypeEnum.START_TIME;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return false;
  }

  isReportEnabled(keyParams: string | undefined, screenId: string): boolean {
    return !!keyParams;
  }
}
